package com.yys.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.yys.gulimall.order.entity.OrderEntity;
import com.yys.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.yys.gulimall.order.constant.OrderConstant.ORDER_RELEASE_QUEUE;

/**
 * @author zq
 * @version 1.0
 * @date 2021/4/6 14:57
 */
@Service
@RabbitListener(queues = ORDER_RELEASE_QUEUE)
@Slf4j
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listenerTest(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        log.info("收到订单取消消息... {}",orderEntity);
        try {
            orderService.closeOrder(orderEntity);
            // TODO 手动调用支付宝收单


            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
